package com.sailfish.springbootdemo.dao.db4;

import com.sailfish.springbootdemo.common.DB4TableName;
import com.sailfish.springbootdemo.controller.db4.Role;
import org.apache.ibatis.annotations.*;
import org.apache.ibatis.type.JdbcType;

import java.util.List;

public interface RoleDao {

    /**
     * 查询所有的角色
     *
     * @return
     */
    @Select("<script>" +
            "select * from " + DB4TableName.ROLE + " where 1 = 1" +
            "<if test='searchRoleName != null and searchRoleName != \"\" '>" +
            " and role_name_cn like CONCAT('%',#{searchRoleName},'%') or role_name_en like CONCAT('%',#{searchRoleName},'%')" +
            "</if>" +
            " order by role_order" +
            "</script>")
    @Results(id = "roleResultMap", value = {
            @Result(column = "role_id", property = "roleId", jdbcType = JdbcType.INTEGER),
            @Result(column = "role_name_cn", property = "roleNameCn", jdbcType = JdbcType.VARCHAR),
            @Result(column = "role_name_en", property = "roleNameEn", jdbcType = JdbcType.VARCHAR),
            @Result(column = "role_order", property = "roleOrder", jdbcType = JdbcType.INTEGER),
            @Result(column = "role_status", property = "roleStatus", jdbcType = JdbcType.INTEGER),
            @Result(column = "role_notes", property = "roleNotes", jdbcType = JdbcType.VARCHAR),
            @Result(column = "create_time", property = "createTime", jdbcType = JdbcType.DATETIMEOFFSET),
            @Result(column = "update_time", property = "updateTime", jdbcType = JdbcType.DATETIMEOFFSET),
            @Result(column = "can_delete", property = "canDelete", jdbcType = JdbcType.INTEGER),
    })
    List<Role> getAllRole(@Param("searchRoleName") String searchRoleName);

    /**
     * 根据id查询角色
     *
     * @param roleId
     * @return
     */
    @Select("select * from " + DB4TableName.ROLE + " where role_id = #{roleId}")
    @ResultMap(value = "roleResultMap")
    Role getRoleById(@Param("roleId") Integer roleId);

    /**
     * 删除
     *
     * @param roleId
     * @return
     */
    @Delete("delete from " + DB4TableName.ROLE + " where role_id = #{roleId}")
    Integer deleteRole(@Param("roleId") Integer roleId);

    /**
     * 插入
     *
     * @param role
     * @return
     */
    @Insert("insert into " + DB4TableName.ROLE + "(role_name_cn,role_name_en,role_order,role_status,role_notes,create_time,can_delete) " +
            "values (#{role.roleNameCn},#{role.roleNameEn},#{role.roleOrder},#{role.roleStatus},#{role.roleNotes},#{role.createTime},#{role.canDelete})")
    Integer addRole(@Param("role") Role role);

    /**
     * 更新
     *
     * @param role
     * @return
     */
    @Update("<script>" +
            "update " + DB4TableName.ROLE +
            "<set>" +
            "<if test='role.roleNameCn != null and role.roleNameCn != \"\" '>" +
            " role_name_cn = #{role.roleNameCn}," +
            "</if>" +
            "<if test='role.roleNameEn != null and role.roleNameEn != \"\" '>" +
            " role_name_en = #{role.roleNameEn}," +
            "</if>" +
            "<if test='role.roleOrder != null and role.roleOrder != \"\" '>" +
            " role_order = #{role.roleOrder}," +
            "</if>" +
            "<if test='role.roleStatus != null and role.roleStatus != \"\" '>" +
            " role_status = #{role.roleStatus}," +
            "</if>" +
            "<if test='role.updateTime != null '>" +
            " update_time = #{role.updateTime}," +
            "</if>" +
            "<if test='role.roleNotes != null and role.roleNotes != \"\" '>" +
            " role_notes = #{role.roleNotes}," +
            "</if>" +
            "</set>" +
            " where role_id = #{role.roleId}" +
            "</script>")
    Integer updateRole(@Param("role") Role role);

    /**
     * 根据 usergroupid 查询
     */
    @Select("select * from " + DB4TableName.ROLE + " where usergroupid = #{usergroupid}")
    @ResultMap(value = "roleResultMap")
    List<Role> getRoleListByUserGroupId(@Param("usergroupid") Integer usergroupid);
}
